---
title: "Figure A.2"
output: 
---

# Figure A.2

# Who's to Blame? Postconflict Violence and Public Attitudes Towards Peace Agreements
# Wyer, Frank. 

#clear environment
```{r clear environment}
rm(list = ls())
```

# uncomment and set working directory to replication archive
# setwd("~/blame_replication")

# Uncomment to install packages if necessary
# install.packages("tidyverse")
# install.packages("cowplot")
# install.packages("estimatr")

#load packages
```{r}
library(tidyverse)
library(estimatr)
library(cowplot)
```

#read in data
```{r}
survey_clean <- read.csv("survey_clean.csv")
```


#releveled treatment variable where postconflict violence condition (T1) is baseline instead of control
```{r treatment with T1 as baseline}
#create treatment variable with T1 as reference instead of control
survey_clean$treatment_vsT1 <- as.factor(survey_clean$treatment)
survey_clean$treatment_vsT1 <- relevel(survey_clean$treatment_vsT1, ref = "T1")
```

#create binary variables for security trajectory and political engagement
```{r generate binary variables}
survey_clean <- survey_clean %>% 
  mutate(homs_worse = ifelse(homratediff > 0, 1, 0), 
         high_engage = ifelse(engage_zscale > median(engage_zscale), 1, 0))
```

#het effects FARC presence, treatment vs t1
```{r het effects: farc presence}
m_farc_no_vt1 <- lm_lin(formula = outcomes_zscale ~ treatment_vsT1, covariates = ~ Q15 + homratediff + urbandummy + Q25 + engage_zscale + factor(regionname), se_type = "HC2", data = survey_clean %>% filter(farc_presence == 0), alpha = .05) %>% tidy() %>% filter(term == "treatment_vsT1T2A" | term == "treatment_vsT1T2B") %>% mutate(farc = "Non-FARC")
m_farc_yes_vt1 <- lm_lin(formula = outcomes_zscale ~ treatment_vsT1, covariates = ~ Q15 + homratediff + urbandummy + Q25 + engage_zscale + factor(regionname), se_type = "HC2", data = survey_clean %>% filter(farc_presence == 1), alpha = .05) %>% tidy() %>% filter(term == "treatment_vsT1T2A" | term == "treatment_vsT1T2B") %>% mutate(farc = "FARC")

het_farc_vt1 <- rbind(m_farc_no_vt1, m_farc_yes_vt1)

het_farc_vt1 <- het_farc_vt1 %>% mutate(Treatment = case_when(
term == "treatment_vsT1T2B" ~ "Rebel Culpability",
term == "treatment_vsT1T2A" ~ "Govt. Culpability",
))

het_farc_vt1$Treatment <- factor(het_farc_vt1$Treatment, levels = c("Rebel Culpability", "Govt. Culpability"))

farc_het_plot_vt1 <- ggplot(het_farc_vt1,  aes(x = estimate, y = farc)) +
    geom_point(position=position_dodge(.5), shape = 16) +
    geom_linerange(aes(y = farc, xmin = conf.low, xmax = conf.high), position=position_dodge(.5)) +
    labs(x = "", y = "", title = "FARC Presence") +
    theme_bw() + 
    xlim(-1.5,1.5) +
    scale_y_discrete(labels = function(x) str_wrap(x, width = 4)) +
    theme(plot.title = element_text(face = "bold", size = 10)) +
    facet_grid(Treatment ~ ., labeller = label_wrap_gen(width = 15, multi_line = TRUE))
```

#het effects engagement, treatment vs T1
```{r het effects: engagement}
m_engage_low_vt1 <- lm_lin(formula = outcomes_zscale ~ treatment_vsT1, covariates = ~ Q15 + homratediff + urbandummy + Q25 + farc_presence + factor(regionname), se_type = "HC2", data = survey_clean %>% filter(high_engage == 0), alpha = .05) %>% tidy() %>% filter(term == "treatment_vsT1T2A" | term == "treatment_vsT1T2B") %>% mutate(engagement = "Low")
m_engage_high_vt1 <- lm_lin(formula = outcomes_zscale ~ treatment_vsT1, covariates = ~ Q15 + homratediff + urbandummy + Q25 + farc_presence + factor(regionname), se_type = "HC2", data = survey_clean %>% filter(high_engage == 1), alpha = .05) %>% tidy() %>% filter(term == "treatment_vsT1T2A" | term == "treatment_vsT1T2B") %>% mutate(engagement = "High")

het_engage_vt1 <- rbind(m_engage_low_vt1, m_engage_high_vt1)

het_engage_vt1$engagement <- factor(het_engage_vt1$engagement, levels = c("Low", "High"))

het_engage_vt1 <- het_engage_vt1 %>% mutate(Treatment = case_when(
term == "treatment_vsT1T2B" ~ "Rebel Culpability",
term == "treatment_vsT1T2A" ~ "Govt. Culpability",
))

het_engage_vt1$Treatment <- factor(het_engage_vt1$Treatment, levels = c("Rebel Culpability", "Govt. Culpability"))

engage_het_plot_vt1 <- ggplot(het_engage_vt1,  aes(x = estimate, y = engagement)) +
    geom_point(position=position_dodge(.5), shape = 16) +
    geom_linerange(aes(y = engagement, xmin = conf.low, xmax = conf.high), position=position_dodge(.5)) +
    labs(x = "", y = "", title = "Political Engagement") +
    theme_bw() + 
    xlim(-1.5,1.5) +
        scale_y_discrete(labels = function(x) str_wrap(x, width = 10)) +
        theme(plot.title = element_text(face = "bold", size = 10)) +
    facet_grid(Treatment ~ ., labeller = label_wrap_gen(width = 15, multi_line = TRUE))
```

#het effects left-right treatment vs t1

```{r het effects df: left vs right}
m_id_right_vt1 <- lm_lin(formula = outcomes_zscale ~ treatment_vsT1, covariates = ~ Q15 + homratediff + urbandummy + engage_zscale + farc_presence + factor(regionname), se_type = "HC2", data = survey_clean %>% filter(Q25 > 3), alpha = .05) %>% tidy() %>% filter(term == "treatment_vsT1T2A" | term == "treatment_vsT1T2B") %>% mutate(ideology = "Right")
m_id_left_vt1 <- lm_lin(formula = outcomes_zscale ~ treatment_vsT1, covariates = ~ Q15 + homratediff + urbandummy + engage_zscale + farc_presence + factor(regionname), se_type = "HC2", data = survey_clean %>% filter(Q25 < 4), alpha = .05) %>% tidy() %>% filter(term == "treatment_vsT1T2A" | term == "treatment_vsT1T2B") %>% mutate(ideology = "Left")

het_id_vt1 <- rbind(m_id_right_vt1, m_id_left_vt1)

het_id_vt1$ideology <- factor(het_id_vt1$ideology, levels = c("Left", "Right"))

het_id_vt1 <- het_id_vt1 %>% mutate(Treatment = case_when(
term == "treatment_vsT1T2B" ~ "Rebel Culpability",
term == "treatment_vsT1T2A" ~ "Govt. Culpability",
))

het_id_vt1$Treatment <- factor(het_id_vt1$Treatment, levels = c("Rebel Culpability", "Govt. Culpability"))

id_het_plot_vt1 <- ggplot(het_id_vt1,  aes(x = estimate, y = ideology)) +
    geom_point(position=position_dodge(.5), shape = 16) +
    geom_linerange(aes(y = ideology, xmin = conf.low, xmax = conf.high), position=position_dodge(.5)) +
    labs(x = "", y = "", title = "Ideology") +
    theme_bw() + 
    xlim(-1.5,1.5) +
    guides(color = "none", shape = "none") +
          scale_y_discrete(labels = function(x) str_wrap(x, width = 10)) +
    theme(plot.title = element_text(face = "bold", size = 10)) +
    facet_grid(Treatment ~ ., labeller = label_wrap_gen(width = 15, multi_line = TRUE))
```

#het effects security: treatment vs control
```{r}
m_sec_worse_vt1 <- lm_lin(formula = outcomes_zscale ~ treatment_vsT1, covariates = ~ Q15 + Q25 + urbandummy + engage_zscale + farc_presence + factor(regionname), se_type = "HC2", data = survey_clean %>% filter(homs_worse == 1), alpha = .05) %>% tidy() %>% filter(term == "treatment_vsT1T2A" | term == "treatment_vsT1T2B") %>% mutate(sec_tercile = "Worsened")
m_sec_better_vt1 <- lm_lin(formula = outcomes_zscale ~ treatment_vsT1, covariates = ~ Q15 + Q25 + urbandummy + engage_zscale + farc_presence + factor(regionname), se_type = "HC2", data = survey_clean %>% filter(homs_worse == 0), alpha = .05) %>% tidy() %>% filter(term == "treatment_vsT1T2A" | term == "treatment_vsT1T2B") %>% mutate(sec_tercile = "Improved")

het_sec_vt1 <- rbind(m_sec_worse_vt1, m_sec_better_vt1)

het_sec_vt1 <- het_sec_vt1 %>% mutate(Treatment = case_when(

term == "treatment_vsT1T2B" ~ "Rebel Culpability",
term == "treatment_vsT1T2A" ~ "Govt. Culpability",
))

het_sec_vt1$Treatment <- factor(het_sec_vt1$Treatment, levels = c("Rebel Culpability", "Govt. Culpability"))

sec_het_plot_vt1 <- ggplot(het_sec_vt1,  aes(x = estimate, y = sec_tercile)) +
    geom_point(position=position_dodge(.5), shape = 16) +
    geom_linerange(aes(y = sec_tercile, xmin = conf.low, xmax = conf.high), position=position_dodge(.5)) +
    labs(x = "", y = "", title = "Security Trajectory") +
    theme_bw() + 
    xlim(-1.5,1.5) +
        scale_y_discrete(labels = function(x) str_wrap(x, width = 10)) +
    theme(plot.title = element_text(face = "bold", size = 10)) +
    facet_grid(Treatment ~ ., labeller = label_wrap_gen(width = 15, multi_line = TRUE))
```

#combine plots 
```{r combined plot}
het_plot_all_vt1 <- plot_grid(farc_het_plot_vt1, sec_het_plot_vt1, id_het_plot_vt1, engage_het_plot_vt1)
```

